package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.nls.common.ConsoleConstant;
import com.fdb.efp.esb.service.bo.req.sed.letter.ReqSedLetterBean;
import com.fdb.efp.esb.service.facade.sed.letter.SedLetterInformService;
import com.fdb.efp.flow.common.vo.PageApproveVO;
import com.fdb.efp.flow.service.facade.BizEventInfoService;
import com.fdb.efp.nls.service.dao.ColltSmsNoticeAppDao;
import com.fdb.efp.nls.service.dao.ColltTaskInfoDao;
import com.fdb.efp.nls.service.domain.ColltSmsNoticeApp;
import com.fdb.efp.nls.service.domain.ColltTaskInfo;
import com.fdb.efp.nls.service.facade.ColltTaskTelSmsFlowService;
import com.fdb.efp.nls.service.vo.ColltSmsNoticeAppVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.fdb.basic.framework.core.util.BeanUtility.beanCopy;


@Service("colltTaskTelSmsFlowService")
public class ColltTaskTelSmsFlowServiceImpl implements ColltTaskTelSmsFlowService {

    private static Logger logger = LoggerFactory.getLogger(ColltTaskTelSmsFlowServiceImpl.class);

    @Autowired
    private ColltSmsNoticeAppDao colltSmsNoticeAppDao;
    @Autowired
    private ColltTaskInfoDao colltTaskInfoDao;
    @Autowired
    @Qualifier("bizEventInfoService")
    private BizEventInfoService bizEventInfoService;

    @Autowired
    @Qualifier("sedLetterInformService")
    private SedLetterInformService sedLetterInformService;

    @Override
    public void pass(String bizSerno, PageApproveVO pageApproveVo) {
        logger.info("电话催收短信通知通过流水号" + bizSerno);
        try {
            if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                ColltSmsNoticeAppVO vo = new ColltSmsNoticeAppVO();
                vo.setSmsNoticeApp(bizSerno);
                ColltSmsNoticeApp colltSmsNoticeApp = new ColltSmsNoticeApp();
                beanCopy(vo,colltSmsNoticeApp);
                colltSmsNoticeApp= colltSmsNoticeAppDao.queryByPk(colltSmsNoticeApp);
                beanCopy(colltSmsNoticeApp,vo);
                if (Objects.nonNull(vo)) {
                    vo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                    vo.setLastUpdateUser(pageApproveVo.getAprvUserId());
                    // 审批用户编号
                    vo.setAprvUserCode(pageApproveVo.getAprvUserId());
                    // 审批意见
                    vo.setAprvComment(pageApproveVo.getAprvComment());
                    // 审批状态 = 通过
                    vo.setApproveStatus(ConsoleConstant.APRV_STATUS_03);
                    // 审批时间
                    vo.setAprvTime(TimeUtil.getCurrentDate());
                }
                beanCopy(vo, colltSmsNoticeApp);
                colltSmsNoticeAppDao.updateByPk(colltSmsNoticeApp);

                //通过后任务状态改为待处理
                ColltTaskInfo colltTaskInfo = new ColltTaskInfo();
                colltTaskInfo.setColltTaskNo(colltSmsNoticeApp.getColltTaskNo());
                ColltTaskInfo tS = colltTaskInfoDao.queryByPk(colltTaskInfo);
                tS.setTaskSts(ConsoleConstant.APRV_STATUS_01);
                colltTaskInfoDao.updateByPk(tS);

                //调用短信接口，通知客户
                this.sendMessageToCus(colltSmsNoticeApp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refuse(String bizSerno, PageApproveVO pageApproveVo) {
        logger.info("电话催收短信通知拒绝流水号" + bizSerno);
        try {
            if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                ColltSmsNoticeAppVO vo = new ColltSmsNoticeAppVO();
                vo.setSmsNoticeApp(bizSerno);
                ColltSmsNoticeApp colltSmsNoticeApp = new ColltSmsNoticeApp();
                beanCopy(vo,colltSmsNoticeApp);
                colltSmsNoticeApp= colltSmsNoticeAppDao.queryByPk(colltSmsNoticeApp);
                beanCopy(colltSmsNoticeApp,vo);
                if (Objects.nonNull(vo)) {
                    vo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                    vo.setLastUpdateUser(pageApproveVo.getAprvUserId());
                    // 审批用户编号
                    vo.setAprvUserCode(pageApproveVo.getAprvUserId());
                    // 审批意见
                    vo.setAprvComment(pageApproveVo.getAprvComment());
                    // 审批状态 = 通过
                    vo.setApproveStatus(ConsoleConstant.APRV_STATUS_04);
                    // 审批时间
                    vo.setAprvTime(TimeUtil.getCurrentDate());
                }
                beanCopy(vo, colltSmsNoticeApp);
                colltSmsNoticeAppDao.updateByPk(colltSmsNoticeApp);

                //拒绝后任务状态改为待处理
                ColltTaskInfo colltTaskInfo = new ColltTaskInfo();
                colltTaskInfo.setColltTaskNo(colltSmsNoticeApp.getColltTaskNo());
                ColltTaskInfo tS = colltTaskInfoDao.queryByPk(colltTaskInfo);
                tS.setTaskSts(ConsoleConstant.APRV_STATUS_01);
                colltTaskInfoDao.updateByPk(tS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void repulse(String bizSerno, PageApproveVO pageApproveVo) {
        try{
            logger.info("电话催收短信通知打回流水号" + bizSerno);
            if(bizEventInfoService.isFirstNode(bizSerno)){
                // 更新打回状态
                if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                    ColltSmsNoticeAppVO vo = new ColltSmsNoticeAppVO();
                    vo.setSmsNoticeApp(bizSerno);
                    ColltSmsNoticeApp colltSmsNoticeApp = new ColltSmsNoticeApp();
                    beanCopy(vo,colltSmsNoticeApp);
                    colltSmsNoticeApp= colltSmsNoticeAppDao.queryByPk(colltSmsNoticeApp);
                    beanCopy(colltSmsNoticeApp,vo);
                    if (Objects.nonNull(vo)) {
                        vo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                        vo.setLastUpdateUser(pageApproveVo.getAprvUserId());
                        // 审批用户编号
                        vo.setAprvUserCode(pageApproveVo.getAprvUserId());
                        // 审批意见
                        vo.setAprvComment(pageApproveVo.getAprvComment());
                        // 审批状态 = 通过
                        vo.setApproveStatus(ConsoleConstant.APRV_STATUS_05);
                        // 审批时间
                        vo.setAprvTime(TimeUtil.getCurrentDate());
                    }
                    beanCopy(vo, colltSmsNoticeApp);
                    colltSmsNoticeAppDao.updateByPk(colltSmsNoticeApp);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private void sendMessageToCus(ColltSmsNoticeApp colltSmsNoticeApp){
        logger.info("======>专项催收申请审批通过后，调用短信通知接口开始<======");
        ReqSedLetterBean reqSedLetterBean = new ReqSedLetterBean();
        reqSedLetterBean.setMblNo(colltSmsNoticeApp.getPhoneNum());
        reqSedLetterBean.setCntntInf(colltSmsNoticeApp.getSendMsg());
        try {
            sedLetterInformService.sendLetter(reqSedLetterBean);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("======>专项催收申请审批通过后，调用短信通知接口失败<======" + e.getMessage());
        } finally {
            logger.info("======>专项催收申请审批通过后，调用短信通知接口开始<======");
        }
    }
}
